package com.lanou.zy.projecta.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.entity.Render;
import com.lanou.zy.projecta.util.DataChange;
import com.lanou.zy.projecta.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/4.
 */
public class RenderRecyclerViewAdapter extends RecyclerView.Adapter<RenderRecyclerViewAdapter.RenderViewHolder>{
    List<Render.DataEntity.EntryEntity> entities;
    private ImageLoader imageLoader;

    public RenderRecyclerViewAdapter(Context c) {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<Render.DataEntity.EntryEntity> datas) {
        this.entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public RenderViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.render_recyclerview_item, null);
        return new RenderViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RenderViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            Render.DataEntity.EntryEntity r = entities.get(position);
            holder.renderCommit_Tv.setText(r.getReplys());
            holder.renderContent_Tv.setText(r.getTitle());
            holder.renderTime_Tv.setText(DataChange.getStrTime(r.getPublish_time() + ""));
            holder.renderUsersName_Tv.setText(r.getUsername());
            holder.renderViews_Tv.setText(r.getViews());
            holder.render_Iv.setImageUrl(r.getPhoto(), imageLoader);
        }
    }


    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ?entities.size() : 0;
    }

    public class RenderViewHolder extends RecyclerView.ViewHolder {
        private TextView renderContent_Tv, renderUsersName_Tv, renderTime_Tv, renderViews_Tv, renderCommit_Tv;
        private NetworkImageView render_Iv;

        public RenderViewHolder(View v) {
            super(v);
            render_Iv = (NetworkImageView) v.findViewById(R.id.render_Iv);
            renderTime_Tv = (TextView) v.findViewById(R.id.renderTime_Tv);
            renderViews_Tv = (TextView) v.findViewById(R.id.renderViews_Tv);
            renderCommit_Tv = (TextView) v.findViewById(R.id.renderCommit_Tv);
            renderContent_Tv = (TextView) v.findViewById(R.id.renderContent_Tv);
            renderUsersName_Tv = (TextView) v.findViewById(R.id.renderUsersName_Tv);

        }
    }
}
